package com.knife.blog.logic.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

//import com.knife.blog.domain.BlogCircle;
import com.knife.blog.domain.BlogCircleMember;
import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.BlogNewsDoc;
import com.knife.blog.logic.BlogCircleService;
import com.knife.util.CommUtil;
import com.knife.web.tools.DbPageList;
import com.knife.web.tools.IPageList;

public class BlogCircleServiceImpl extends DAOSupportService implements
		BlogCircleService {

	private static BlogCircleServiceImpl singleton = new BlogCircleServiceImpl();
	 private boolean keywordCanSplit = false;
	public static BlogCircleServiceImpl getInstance() {
		return singleton;
	}

	public boolean addBlogCircle(BlogInfo circle) {
		boolean ret = false;
		circle.setUpdated(Boolean.TRUE);
		ret = this.dao.save(circle);
		return ret;
	}

	public boolean addBlogCircleMember(BlogCircleMember member) {
		boolean ret = false;
		ret = this.dao.save(member);
		return ret;
	}

	public int batchDelBlogCircle(String[] cids) {
		int ret = 0;
		for (int i = 0; i < cids.length; i++) {
			if (delBlogCircle(cids[i]))
				ret++;
		}
		return ret;
	}
	public int batchDelBlogCircleByID(String[] cids) {
		int ret = 0;
		for (int i = 0; i < cids.length; i++) {
			if (delBlogCircleByID(cids[i]))
				ret++;
		}
		return ret;
	}
	public int batchDelBlogCircleMember(String[] cids) {
		int ret = 0;
		for (int i = 0; i < cids.length; i++) {
			if (delBlogCircleMember(cids[i]))
				ret++;
		}
		return ret;
	}

	public boolean delBlogCircle(String cid) {
		boolean ret = false;
		this.dao.del(getBlogCircle(cid));
		return ret;
	}
	public boolean delBlogCircleByID(String cid) {
		boolean ret = false;
		this.dao.del(getBlogCircleByCid(cid));
		return ret;
	}

	public boolean delBlogCircleMember(String cid) {
		boolean ret = false;
		this.dao.del(getBlogCircleMember(cid));
		return ret;
	}

	public BlogInfo getBlogCircle(String inputuser) {
		return (BlogInfo)this.dao.getBy(BlogInfo.class, "inputuser", inputuser);
	}
	
	public BlogInfo getBlogCircle(String fieldName,String value){
		return (BlogInfo)this.dao.getBy(BlogInfo.class, fieldName, value);
	}
	
	public BlogInfo getBlogCircleByCid(String cid){
		return (BlogInfo)this.dao.get(BlogInfo.class, cid);
	}

	public BlogCircleMember getBlogCircleMember(String cid) {
		return (BlogCircleMember) this.dao.get(BlogCircleMember.class, cid);
	}
	
	public List getTopCircle(String orderBy,int num){
		String scope = "type='circleBlog'";
		if(!(orderBy.equals("")&&orderBy!=null)){
			scope += " order by " + orderBy + " desc";
		}else{
			scope += " order by inputtime desc";
		}
		DbPageList pList = new DbPageList(BlogInfo.class, scope);
		pList.doList(0, num);
		return pList.getResult();
	}

	public void joinBlogCircle(BlogInfo circle, BlogCircleMember member) {
		// TODO Auto-generated method stub
		throw new java.lang.IllegalArgumentException("未完成的功能!");
	}

	public IPageList queryBlogCircle(Collection paras , String scope , int currentPage, int pageSize) {
		//String scope = "inputuser=?";
		DbPageList pList = new DbPageList(BlogInfo.class, scope, paras);
		pList.doList(currentPage, pageSize);
		return pList;
	}
	
	public IPageList queryBlogCircle(String username, String circleSn, String dirId,
			Date date1, Date date2, int currentPage, int pageSize){
		String scope = "1=1";
		java.util.Collection paras = new java.util.ArrayList();
		if (username != null && (!"".equals(username))) {
			scope = "inputuser=? ";
			paras.add(username);
		}
		if(circleSn!=null && (!"".equals(circleSn))){
			scope = "circleSn=? ";
			paras.add(circleSn);
		}
		if (dirId != null && (!"".equals(dirId))) {
			scope += " and dirSn=?";
			paras.add(dirId);
		}
		if (date1 != null) {
			scope += " and displaytime>=?";
			paras.add(date1);
		}
		if (date2 != null) {
			scope += " and displaytime<?";
			paras.add(date2);
		}
		scope += " order by displaytime desc";
		DbPageList pList = new DbPageList(BlogInfo.class, scope, paras);
		pList.doList(currentPage, pageSize);
		return pList;
	}
	public IPageList queryBlogCircle(String circleSn, String dirId,
			Date date1, Date date2, int currentPage, int pageSize){
		String scope = "type='circleBlog'";
		java.util.Collection paras = new java.util.ArrayList();
		if(circleSn!=null && (!"".equals(circleSn))){
			scope = "circleSn=? ";
			paras.add(circleSn);
		}
		if (dirId != null && (!"".equals(dirId))) {
			scope += " and dirSn=?";
			paras.add(dirId);
		}
		if (date1 != null) {
			scope += " and inputtime>=?";
			paras.add(date1);
		}
		if (date2 != null) {
			scope += " and inputtime<?";
			paras.add(date2);
		}
		scope += " order by inputtime desc";
		DbPageList pList = new DbPageList(BlogInfo.class, scope, paras);
		pList.doList(currentPage, pageSize);
		return pList;
	}
	public IPageList queryBlogCircle(String circleSn, String dirId,
			Date date1, Date date2, int currentPage, int pageSize,String query){
		String scope = "type='circleBlog'";
		java.util.Collection paras = new java.util.ArrayList();
		if(circleSn!=null && (!"".equals(circleSn))){
			scope = "circleSn=? ";
			paras.add(circleSn);
		}
		if (dirId != null && (!"".equals(dirId))) {
			scope += " and dirSn=?";
			paras.add(dirId);
		}
		if (date1 != null) {
			scope += " and inputtime>=?";
			paras.add(date1);
		}
		if (date2 != null) {
			scope += " and inputtime<?";
			paras.add(date2);
		}
		if (query != null && (!"".equals(query))) {
	        query=query.trim();
	        query=query.replace("　", " ");
	        query=query.replace("｜", "|");
	        query=query.replace("，", ",");
	        scope += splitKeyWords(query," ");
	        scope += splitKeyWords(query,"|");
	        scope += splitKeyWords(query,",");
	        if(!keywordCanSplit){
	            scope += " and title like '%" + query + "%'";
	        }
	        //paras.add(query);
	    }
		scope += " order by inputtime desc";
		DbPageList pList = new DbPageList(BlogInfo.class, scope, paras);
		pList.doList(currentPage, pageSize);
		return pList;
	}
	private String splitKeyWords(String keywords, String seperator) {
        String keywordsSql = "";
        keywords=keywords.trim();
        if (keywords.contains(seperator)) {
            keywordCanSplit=true;
            String[] myquery = keywords.split(seperator);
            for (int i = 0; i < myquery.length; i++) {
                keywordsSql += " and title like '%" + myquery[i] + "%'";
            }
        }
        return keywordsSql;
    }
	

	public IPageList queryBlogCircleMember(BlogInfo circle, int currentPage,
			int pageSize) {
		String scope = "sn=?";
		Collection paras = new java.util.ArrayList();
		paras.add(circle.getCid());
		DbPageList pList = new DbPageList(BlogCircleMember.class, scope, paras);
		pList.doList(currentPage, pageSize);
		return pList;
	}
	
	public IPageList queryAllUserCircle(String username, int currentPage, int pageSize){
		String scope = "type='circleBlog' and inputuser=? or cid in(select sn as cid from blogcirclemember where inputuser=?)";
		Collection paras = new java.util.ArrayList();
		paras.add(username);
		paras.add(username);
		return queryBlogCircle(scope,paras,currentPage,pageSize);
	}
	
	public IPageList queryAllUserCircle(String username){
		return queryAllUserCircle(username,0,-1);
	}

	/*public List queryBlogCircle(String username) {
		String scope = "cid in (select parentId as cid from BlogCircleMember where username=?)";
		Collection paras = new java.util.ArrayList();
		paras.add(username);
		return this.dao.query(BlogCircle.class, scope, paras);
	}*/

	public boolean updateBlogCircle(BlogInfo circle) {
		boolean ret = false;
		ret = this.dao.update(circle);
		return ret;
	}

	public boolean updateBlogCircleMember(BlogCircleMember member) {
		boolean ret = false;
		ret = this.dao.update(member);
		return ret;
	}

	public IPageList queryBlogNewsDoc(BlogInfo circle, String username,
			int status, Date date1, Date date2, String orderBy,
			int currentPage, int pageSize) {
		String scope = "cid in ( select docid as cid from blogcirclenewsdoc where 1=1 ";
		java.util.Collection paras = new java.util.ArrayList();
		if (username != null && (!"".equals(username))) {
			scope += "and circlesn in(select sn as circlesn from blogcirclemember where inputuser=?) ";
			paras.add(username);
		}
		if (circle != null) {
			scope += " and circleSn=?";
			paras.add(circle.getCid());
		}
		if (status > 0) {
			scope += "status>=" + status;
		}
		scope += ")";
		if (date1 != null) {
			scope += " and displaytime>=?";
			paras.add(date1);
		}
		if (date2 != null) {
			scope += " and displaytime<?";
			paras.add(date2);
		}
		if (!CommUtil.hasLength(orderBy))
			scope += " order by displaytime desc";
		else
			scope += " order by " + orderBy;
		DbPageList pList = new DbPageList(BlogNewsDoc.class, scope, paras);
		pList.doList(currentPage, pageSize);
		return pList;
	}

	public IPageList queryBlogCircle(String scope, Collection paras, int currentPage, int pageSize) {
		DbPageList pList = new DbPageList(BlogInfo.class, scope, paras);
		pList.doList(currentPage, pageSize);
		return pList;
	}


	public List queryBlogCircle(String parentCid) {
		String scope = "dirsn=?";
		Collection paras = new java.util.ArrayList();
		paras.add(parentCid);
		return this.dao.query(BlogInfo.class, scope, paras);
	}

	public IPageList queryUserCircle(String username) {
		String scope = "cid in (select sn as cid from blogcirclemember where inputuser=?)";
		//String scope = "inputuser=?";
		Collection paras = new java.util.ArrayList();
		paras.add(username);
		DbPageList pList = new DbPageList(BlogInfo.class, scope, paras);
		pList.doList(0, -1);
		return pList;
	}
	
	public IPageList queryOnwerCircle(String username){
		String scope = "inputuser=?";
		Collection paras = new java.util.ArrayList();
		paras.add(username);
		DbPageList pList = new DbPageList(BlogInfo.class, scope, paras);
		pList.doList(0, -1);
		return pList;
	}

	public List queryBlogCircle() {
		String scope = "type='circleBlog'";
		DbPageList pList = new DbPageList(BlogInfo.class, scope);
		pList.doList(0, -1);
		return pList.getResult();
	}

	/*public IPageList queryDoc(String scope,int currentPage, int pages) {
		// 
		java.util.Collection paras = new java.util.ArrayList();
		DbPageList pList = new DbPageList(BlogCircleDoc.class,scope,paras);
		pList.doList(currentPage, pages);
		return pList;
	}*/

}
